package com.example.miniodemo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.miniodemo.common.commondto.file.FileDownloadDTO;
import com.example.miniodemo.common.result.Result;
import com.example.miniodemo.common.result.ResultEnum;
import com.example.miniodemo.common.service.FileApi;
import com.example.miniodemo.common.service.SystemFileService;
import com.example.miniodemo.common.util.AssertUtils;
import com.example.miniodemo.mapper.ArticleMapper;
import com.example.miniodemo.pojo.domain.Article;
import com.example.miniodemo.pojo.domain.SystemFile;
import com.example.miniodemo.pojo.dto.ArticleAddOrEditDTO;
import com.example.miniodemo.pojo.dto.ArticleListDTO;
import com.example.miniodemo.pojo.vo.ArticleDetailResultVO;
import com.example.miniodemo.service.ArticleService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 文章 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2022/11/11
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private SystemFileService systemFileService;

    /**
     * 添加文章
     *
     * @param articleAddOrEditDTO 文章添加对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> add(ArticleAddOrEditDTO articleAddOrEditDTO) {
        // 处理文件并获取文件id并做校验
        int fileId = systemFileService.upload(articleAddOrEditDTO.getMultipartFile());
        AssertUtils.notNull(fileId, ResultEnum.NOT_EXIST_FILE);
        // 持久化数据
        Article article = modelMapper.map(articleAddOrEditDTO, Article.class);
        article.setPublishTime(new Date());
        article.setFileId(fileId);
        articleMapper.insert(article);
        return Result.success(article.getId());
    }

    /**
     * 获取文章详情
     *
     * @param id 文章id
     * @return
     */
    @Override
    public Result<?> get(Integer id) {
        Article article = articleMapper.selectById(id);
        AssertUtils.notNull(article, ResultEnum.DATA_NOT_EXIST);
        // 获取文件信息
        SystemFile systemFile = systemFileService.get(article.getFileId());
        AssertUtils.notNull(systemFile, ResultEnum.DATA_ERROR);

        // 封装返回数据
        ArticleDetailResultVO articleDetailVO = new ArticleDetailResultVO();
        articleDetailVO.setId(article.getId());
        articleDetailVO.setArticleTitle(article.getArticleTitle());
        articleDetailVO.setArticleContent(article.getArticleContent());
        articleDetailVO.setArticleAuthor(article.getArticleAuthor());
        articleDetailVO.setPublishTime(article.getPublishTime());
        articleDetailVO.setFileId(article.getFileId());
        articleDetailVO.setFileName(systemFile.getFileName());
        articleDetailVO.setFileUrl(systemFile.getFileUrl());

        return Result.success(articleDetailVO);
    }

    /**
     * 查询文章列表
     *
     * @param articleListDTO 文章查询对象
     * @return
     */
    @Override
    public Result<?> list(ArticleListDTO articleListDTO) {
        IPage<Article> page = new Page<>(articleListDTO.getPageNo(), articleListDTO.getPageSize());
        IPage<Article> articlePage = articleMapper.selectByTitleAndAuthorAndPublishTime(page, articleListDTO.getArticleTitle(), articleListDTO
                .getArticleAuthor(), articleListDTO.getMinPublishTime(), articleListDTO.getMaxPublishTime());
        return Result.success(articlePage);
    }

    @Autowired
    private FileApi fileApi;

    /**
     * 下载文章
     *
     * @param fileDownloadDTO 文件下载数据传输对象
     * @return
     */
    @Override
    public Result<?> download(FileDownloadDTO fileDownloadDTO) {
        // 获取文件相关信息
        Article article = articleMapper.selectById(fileDownloadDTO.getBusinessId());
        AssertUtils.notNull(article, ResultEnum.DATA_NOT_EXIST);
        SystemFile systemFile = systemFileService.get(article.getFileId());
        AssertUtils.notNull(systemFile, ResultEnum.DATA_ERROR);

        // 下载操作
        fileApi.download(systemFile.getFileName(), fileDownloadDTO.getFileDownloadPath());
        return Result.success();
    }

    /**
     * 修改文章信息
     *
     * @param id                  文章id
     * @param articleAddOrEditDTO 文章修改对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> edit(Integer id, ArticleAddOrEditDTO articleAddOrEditDTO) {
        // 文件处理
        Integer fileId = systemFileService.upload(articleAddOrEditDTO.getMultipartFile());
        // 模型转换
        Article article = modelMapper.map(articleAddOrEditDTO, Article.class);
        article.setId(id);
        article.setFileId(fileId);
        int count = articleMapper.updateById(article);
        return count > 0 ? Result.success(count) : Result.failed();
    }
}
